<?php

/**
 * XFramework
 *
 * @author Diogo Dias Barreiros
 */
if (!class_exists('InvalidLinkException')) {

    final class InvalidLinkException extends Exception {

        public function __construct($controllerName, $actionName, $params) {
            $page = $controllerName;
            if (!empty($actionName)) {
                $page .= '/' . $actionName;
                if (!empty($params)) {
                    $page .= '/' . implode('/', $params);
                }
            }
            $this->message = "Invalid action call. Trying to call '{$page}'.";
            $this->page = $page;
        }

    }

}

final class URL {

    private static $instance = null;
    private $controller = null;
    private $action = null;
    private $params = array();
    private $requestURI = null;

    private function __construct() {
        $this->parseURL();
    }

    public static function getInstance() {
        if (!isset(self::$instance)) {
            self::$instance = new self();
        }
        return self::$instance;
    }

    public function parseURL() {
        if (isset($_GET['cmd'])) {
            $usableURI = $_GET['cmd'];
            if ($length = strlen(SITE_ROOT)) {
                if (strtolower(SITE_ROOT) == strtolower(substr($usableURI, 0, $length))) {
                    $usableURI = substr($usableURI, $length);
                }
            }
        } else {
            $usableURI = null;
        }
        $this->requestURI = $usableURI;
        if (strlen($usableURI)) {
            if (substr($usableURI, -1) != '/') {
                header("Location: {$usableURI}/");
                die;
            }
            $explodeBar = explode('/', $usableURI);
            if (!empty($explodeBar[0])) {
                $this->controller = ucfirst(Inflector::getInstance()->variable($explodeBar[0]));
                unset($explodeBar[0]);
                if (!empty($explodeBar[1])) {
                    $this->action = strtolower($explodeBar[1]);
                    while ($this->action[0] == '_') {
                        $this->action = substr($this->action, 1);
                    }
                    unset($explodeBar[1]);
                    if (!empty($explodeBar[2])) {
                        $this->params = array_values($explodeBar);
                    }
                }
            }
        }
        if (!isset($this->controller)) {
            $this->controller = Config::get('App.defaultLayout', false);
        }
    }

    public function getController() {
        $Inflector = Inflector::getInstance();
        return $Inflector->classify($this->controller) . 'Controller';
    }

    public function getAction() {
        return (isset($this->action) ? $this->action : '_') . 'Action';
    }

    public function getParams() {
        return $this->params;
    }
    
    public function createUrl(array $path) {
        $url = SITE_ROOT;
        if (isset($path['controller'])) {
            $url .= $path['controller'].'/';
            if (isset($path['action'])) {
                $url .= $path['action'].'/';
                if (isset($path['params'])) {
                    $url .= implode('/', $path['params']);
                }
            }
        }
        return $url;
    }
    
    public function reload() {
        echo '<script type="text/javascript">window.location = window.location;</script>';die;
    }

}

?>
